package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Tarefa;
import dao.TarefaDao;

/**
 * Servlet implementation class modificarStatusTarefa
 */
public class modificarStatusTarefa extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public modificarStatusTarefa() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] tarefasS = request.getParameterValues("tarefaPendentes");
		List<Long> tarefas = new ArrayList<Long>();
		for(int i=0; i< tarefasS.length; i++){
			 tarefas.add(Long.parseLong(tarefasS[i])) ;
		}

		List<Tarefa> tarefaMudar = new ArrayList<Tarefa>();
		if(tarefas!=null){
			for(int i=0; i< tarefas.size(); i++)
			{
			if(TarefaDao.buscarTarefaPorId(tarefas.get(i)) != null)
			tarefaMudar.add(TarefaDao.buscarTarefaPorId(tarefas.get(i)));
			
			}
			
			for(int i = 0; i < tarefaMudar.size(); i++){
				tarefaMudar.get(i).setPendente(false);
				TarefaDao.modificarTarefa(tarefaMudar.get(i));
			}
		}
		RequestDispatcher rd = request.getRequestDispatcher("listaDeTodasTarefas.jsp");
		rd.include(request, response);
	}

}
